# 导包
import unittest
from api.login import LoginAPI
from api.register import RegisterAPI
import requests
from parameterized import parameterized
from common.readjson import read_json_approve
from bs4 import BeautifulSoup


# 创建测试类
class TestRegisterAPI(unittest.TestCase):
    # 类前置处理
    @classmethod
    def setUpClass(cls) -> None:
        # 实例化session对象
        cls.session = requests.session()
        # 实例化接口类（登录、开户）
        cls.login_api = LoginAPI()
        cls.register_api = RegisterAPI()
        # 登录成功
        response = cls.login_api.login666(cls.session, {"keywords": "13611160006", "password": "a123123"})
        print(response.json())

    # 类后置处理
    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    # 实名认证
    @parameterized.expand(read_json_approve())
    def test01_approve_real_name(self, realname, card_id):
        approve_data = {
            "realname": realname,
            "card_id": card_id
        }
        print(approve_data)
        # P2P项目上没有实现真正的实名认证接口（保留了图片上传进行验证代码）
        files = {
            "key": "value"
        }
        response = self.register_api.approve_real_name(self.session, approve_data, files)
        print(response.url)
        print(response.text)

    # 获取认证信息
    # def test02_get_approve(self):
    #     resp = self.register_api.get_approve(self.session)
    #     print(resp.json())
    def test02_get_approve(self):
        resp = self.register_api.get_approve(self.session)
        print(resp.json())

    # 开户
    def test03_post_register(self):
        resp = self.register_api.post_register(self.session)
        # print(resp.json())

        # 获取HTML信息
        html = resp.json().get('description').get('form')
        print(html)
        # 解析HTML数据
        suop = BeautifulSoup(html,'html.parser')

        # 提取url信息
        print('*'*50)
        url = suop.form.get('action')
        print(url)

        # 提取请求方法
        method = suop.form.get('method')
        print(method)

        # 提取请求数据
        request_data = {}
        for inout_data in suop.find_all('input'):
            name = inout_data.get('name')
            value = inout_data.get('value')
            # 把键与值添加到字典
            request_data[name] = value
        print(request_data)

        # 发送第三方开户请求-----url和data是从上面获取的
        resp_3 = requests.post(url=url,data=request_data)
        print(resp_3.text)
        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json().get('status'))




if __name__ == '__main__':
    unittest.main()

























